package com.zs.leymovie.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.R;
import com.zs.leymovie.activity.XuanZuoActivity;
import com.zs.leymovie.bean.EveryDay;
import com.zs.leymovie.constants.UrlConstants;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/8/19.
 */
public class YyItemPtrAdapter extends AppAdapter<EveryDay> {
    private String city_id;
    private int flag = 0;
    private int nextFlag = 0;
    private static boolean morning = true;
    private static boolean noon = true;
    private static boolean night = true;
    private static String moring_time;
    private static String noon_time;
    private static String night_time;

    private String show_id;

    public YyItemPtrAdapter(List<EveryDay> list, Context context, String city_id) {
        super(list, context);
        this.city_id = city_id;
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.yyxq_ptrlist_item, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        final EveryDay day = list.get(position);
        if(position == 0){
            if (show_id == null || TextUtils.equals(show_id, "") || show_id.length() <= 0) {
                show_id = day.getShow_id();
            } else {
                if (!TextUtils.equals(show_id,day.getShow_id())) {
                    show_id = day.getShow_id();
                    morning = true;
                    noon = true;
                    night = true;
                    moring_time = null;
                    noon_time = null;
                    night_time = null;
                }
            }
        }

        String start_time = day.getStart_time();
        int hour = getHour(start_time);
        if (position == 0) {
            if ((hour >= 18 || hour < 6)) {
                flag = 1;
                night = false;
                vh.changci_icon_sunrise.setVisibility(View.VISIBLE);
                vh.changci_icon_sunrise.setTag(start_time);
                if (night_time == null) {
                    night_time = start_time;
                }
                vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_moon);
            } else if (hour >= 12) {
                flag = 2;
                noon = false;
                vh.changci_icon_sunrise.setVisibility(View.VISIBLE);
                vh.changci_icon_sunrise.setTag(start_time);
                if (noon_time == null) {
                    noon_time = start_time;
                }

                vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_sun);
            } else {
                flag = 3;
                morning = false;
                vh.changci_icon_sunrise.setVisibility(View.VISIBLE);
                vh.changci_icon_sunrise.setTag(start_time);
                if (moring_time == null) {
                    moring_time = start_time;
                }
                vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_sunrise);
            }
        } else {
            if (hour >= 18 || hour < 6) {
                nextFlag = 1;
            } else if (hour >= 12) {
                nextFlag = 2;
            } else {
                nextFlag = 3;
            }

            if (flag == nextFlag) {
                switch (nextFlag) {
                    case 1:
                        vh.changci_icon_sunrise.setVisibility(View.GONE);
                        vh.changci_icon_sunrise.setTag(null);
                        break;
                    case 2:
                        vh.changci_icon_sunrise.setVisibility(View.GONE);
                        vh.changci_icon_sunrise.setTag(null);
                        break;
                    case 3:
                        vh.changci_icon_sunrise.setVisibility(View.GONE);
                        vh.changci_icon_sunrise.setTag(null);
                        break;
                }
            } else {
                flag = nextFlag;
                switch (nextFlag) {
                    case 1:
                        if (night) {
                            night = false;
                            if (night_time == null) {
                                night_time = start_time;
                            }
                            vh.changci_icon_sunrise.setTag(start_time);
                            vh.changci_icon_sunrise.setVisibility(View.VISIBLE);
                            vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_moon);
                        } else {
                            vh.changci_icon_sunrise.setTag(null);
                            vh.changci_icon_sunrise.setVisibility(View.GONE);
                            vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_moon);
                        }
                        break;
                    case 2:
                        if (noon) {
                            noon = false;
                            if (noon_time == null) {
                                noon_time = start_time;
                            }
                            vh.changci_icon_sunrise.setTag(start_time);
                            vh.changci_icon_sunrise.setVisibility(View.VISIBLE);
                            vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_sun);
                        } else {
                            vh.changci_icon_sunrise.setTag(null);
                            vh.changci_icon_sunrise.setVisibility(View.GONE);
                            vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_sun);
                        }
                        break;
                    case 3:
                        if (morning) {
                            morning = false;
                            if (moring_time == null) {
                                moring_time = start_time;
                            }
                            vh.changci_icon_sunrise.setTag(start_time);
                            vh.changci_icon_sunrise.setVisibility(View.VISIBLE);
                            vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_sunrise);
                        } else {
                            vh.changci_icon_sunrise.setTag(null);
                            vh.changci_icon_sunrise.setVisibility(View.GONE);
                            vh.changci_icon_sunrise.setImageResource(R.mipmap.changci_icon_sunrise);
                        }
                        break;
                }
            }
        }
        if (TextUtils.equals(moring_time, start_time) || TextUtils.equals(night_time, start_time) || TextUtils.equals(noon_time, start_time)) {
            vh.changci_icon_sunrise.setVisibility(View.VISIBLE);
        } else {
            vh.changci_icon_sunrise.setVisibility(View.GONE);
        }
        Log.e("111111111111111111", "night_time" + night_time + ",noon_time" + noon_time + ",moring_time" + moring_time+",position"+position);
        vh.starttime.setText(start_time);
        vh.endtime.setText(day.getEnd_time() + "散场");
        vh.handname.setText(day.getHall_name());
        vh.lang.setText(day.getLanguage() + "/" + day.getMedia());
        vh.origi_price.setText(day.getOrigi_price());
        vh.origi_price.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);  //中划线
        vh.price.setText(day.getPrice());
        vh.select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, XuanZuoActivity.class);
                intent.putExtra(UrlConstants.SHOW_ID, day.getShow_id());
                intent.putExtra(UrlConstants.CITY_ID, city_id);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public static int getHour(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date date = null;
        int hour = 8;
        try {
            date = sdf.parse(strDate);
            hour = date.getHours();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return hour;
    }

    public static class ViewHolder {
        @ViewInject(R.id.view_line2)
        private View view_line2;
        @ViewInject(R.id.changci_icon_sunrise)
        private ImageView changci_icon_sunrise;
        @ViewInject(R.id.itemptr_starttime)
        private TextView starttime;
        @ViewInject(R.id.itemptr_endtime)
        private TextView endtime;
        @ViewInject(R.id.itemptr_lang)
        private TextView lang;
        @ViewInject(R.id.itemptr_price)
        private TextView price;
        @ViewInject(R.id.itemptr_origiprice)
        private TextView origi_price;
        @ViewInject(R.id.itemptr_handname)
        private TextView handname;
        @ViewInject(R.id.itemptr_select)
        private Button select;

        public ViewHolder(View itemview) {
            ViewUtils.inject(this, itemview);
        }
    }
}
